package com.ebt.m.apps;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.activity.WebBaseActivity;
import com.ebt.m.apps.response.ModulesBean;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.commons.buscomponent.listview.BaseRecyclerAdapter;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.data.bean.CardInfo;
import com.ebt.m.data.bean.UserInfo;
import com.ebt.m.proposal_v2.ui.ActProposalList;
import com.ebt.m.utils.ConfigData;
import com.ebt.m.utils.TalkingDataUtil;
import com.ebt.m.utils.UIHelper;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * Created by Damon on 2017/10/26.
 */

public class AppsView extends BaseListView<AppsPresenter> implements BaseRecyclerAdapter.SpanSizeCallback {

    public static final int TITLE_VIEW_YTPE = 0; //展业应用
    public static final int APPS_VIEW_TYPE = 1; //服务查询
    public static final int SERVICE_VIEW_TYPE = 2;//招募工具
    /**
     * 备注：3为认证注册保留
     */
    public static final int JUNBAOGE_VIEW_TYPE = 4;

    public AppsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppsView(Context context) {
        this(context, null);
    }

    public AppsView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mRefreshLayout.setRefreshNeeded(false);
        mLineAdpater.setSpanSizeCallback(this);
    }

    public IView.OnClickListner onClickListner = new IView.OnClickListner() {
        @Override
        public void onClick(View view, Object... args) {
            ModulesBean modulesBean = (ModulesBean) args[0];
            if (modulesBean == null) {
                return;
            }
            if (modulesBean.getStatus() <= 1) {
                UIHelper.makeToast(getContext(), getContext().getString(R.string.str_wait));
                return;
            }

            String url = modulesBean.getModuleUrl();
            String title = modulesBean.getModuleName();
            Log.e("modulesBean==",modulesBean.getModuleName());
            if (title.equals("计划书")) {
                Intent intent = new Intent(getContext(), ActProposalList.class);
                getContext().startActivity(intent);
                TalkingDataUtil.onEventWithAgentId(TalkingDataUtil.TYPE_APP_PROPOSAL_V270);
            } else if (title.equals("寿险百科")) {
                title = "寿险百科";
                url = ConfigData.WIKI_URL + "?isBack=true";
                gotoWeb(false, title, url, true);
                TalkingDataUtil.onEventWithAgentId(TalkingDataUtil.TYPE_APP_WIKI_V270);
            } else if (title.equals("在线客服")) {
                CardInfo cardInfo = AppContext.getCurrentUser().getCardInfo();
                String param = "";
                if (cardInfo != null) {
                    String sexName = cardInfo.getSexValue();
                    String licenceNo = TextUtils.isEmpty(cardInfo.getLicenseNo()) ? "" : cardInfo.getLicenseNo();
                    try {
                        param = "&sex=" + URLEncoder.encode(sexName, "utf-8") + "&certif_type=" + URLEncoder.encode("执业证", "utf-8") + "&certif=" + licenceNo;
                    } catch (UnsupportedEncodingException e) {
//                        Logger.e(e.getMessage());
                    }
                }
//                Logger.i(param);
                gotoWeb(true, title, url + param, true);
                TalkingDataUtil.onEventWithAgentId(TalkingDataUtil.TYPE_APP_WIKI_V270);
            } else {
                gotoWeb(true, title, url, false);

                HashMap kv = new HashMap();
                UserInfo userInfo = AppContext.getCurrentUser();
                kv.put("agentId", String.valueOf(userInfo.getUserId()));
                String event = modulesBean.getModuleType() == 1 ? TalkingDataUtil.TYPE_APP_ZHANYE_V270 : TalkingDataUtil.TYPE_APP_SERVICE_V270;
                TalkingDataUtil.onEventWithParamsAndLabel(event, kv, modulesBean.getModuleName());
            }
        }

        @Override
        public void onLongClick(View view, Object... args) {

        }
    };

    @Override
    protected ISectionFactory createViewFactory() {
        return viewType -> {
            if (viewType == TITLE_VIEW_YTPE) {
                return new AppsViewTitle(getContext());
            } else if (viewType == APPS_VIEW_TYPE || viewType == SERVICE_VIEW_TYPE || viewType == JUNBAOGE_VIEW_TYPE) {
                AppsViewItem appsViewItem = new AppsViewItem(getContext());
                appsViewItem.setOnClickViewListener(onClickListner);
                return appsViewItem;
            }
            return null;
        };
    }

    private void gotoWeb(boolean isHideTitle, String title, String url, boolean isHideToolbar) {
        Intent webIntent = new Intent(getContext(), WebBaseActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(WebBaseActivity.WEB_TYPE_TITLE, isHideTitle ? "" : title);
        bundle.putInt(WebBaseActivity.WEB_TYPE_FRAGMENT, 111);
        bundle.putBoolean(WebBaseActivity.WEB_TYPE_HIDE_TOOLBAR, isHideToolbar);
        bundle.putString(WebBaseActivity.WEB_TYPE_LINK, url);
        webIntent.putExtras(bundle);
        getContext().startActivity(webIntent);
    }

    @Override
    protected AppsPresenter createPresenter() {
        return new AppsPresenter(getContext(), this);
    }

    @Override
    public void loadNext() {

    }

    @Override
    public void update(Object... args) {
        super.update(args);
        mPresenter.loadNew(args);
    }

    @Override
    public boolean createFootView() {
        return false;
    }

    @Override
    public int getSpanSize(int type) {
        switch (type) {
            case TITLE_VIEW_YTPE:
                return 4;
            case APPS_VIEW_TYPE:
                return 1;
            case SERVICE_VIEW_TYPE:
                return 1;
            default:
                return 1;
        }
    }


    @Override
    protected RecyclerView.LayoutManager generateLayoutManager() {
        return new GridLayoutManager(getContext(), 4);
    }

}
